package com.baimuzhi.newcreate.aop;

import com.baimuzhi.newcreate.utils.JsonResoult;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * @ClassName : InsAop
 * @Description : 审查切面，检查参数有没有违规
 * @Author : 肖叶茂
 * @Date: 2023/1/14  11:32
 */
@Component
@Aspect
public class InsAop {

    //获取日志对象
    static final Logger logger = LoggerFactory.getLogger(Logger.class);


    @Around("execution(* com.baimuzhi.newcreate.controller..*(..))")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Object obj = null;
        logger.debug("执行方法: "+joinPoint.toLongString());
        Object[] argObjects =  joinPoint.getArgs();
        if(argObjects.length != 0){
            for(int i = 0;i<argObjects.length;i++){
              //检查参数
                if(checkArg(argObjects[i])){
                    //返回为true，继续执行函数并获取到返回值
                   obj =   process(joinPoint);
                  if(obj != null){
                      logger.debug("执行成功,结果为: "+ obj.toString());
                  }

                }else {
                    JsonResoult<String> res = new JsonResoult<>(500,"参数违规,审查器已经拦截","");
                   
                    logger.debug("参数 "+argObjects[i].toString()+" 违规，已经拦截执行");
                    return res;
                }
            }

        }
        return obj;
    }


    /**
    *@Description:如果返回false就是参数违规
    *@Parameter:[obj]
    *@Return:java.lang.Boolean
    *@Author:肖叶茂
    *@Date:2023/1/14
    **/
    private Boolean checkArg(Object obj){
        Boolean flag = true;

        //下面判断参数是否违规

        //该段判断string类型的
        if(obj instanceof  String){

            //这里先静态，后面进行数据库操作

            if(obj.toString().equals("傻逼")){
                flag = false;
            }
        }
        return flag;
    }


    /**
    *@Description:继续执行操作并且返回值
    *@Parameter:[]
    *@Return:java.lang.Object
    *@Author:肖叶茂
    *@Date:2023/1/14
    **/
    private Object process(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
      Object obj =  proceedingJoinPoint.proceed();
      return obj;
    }
}
